﻿using System;
using System.Data;
using System.Windows.Forms;
using DevExpress.XtraEditors;
//using UniMana.Lib.Personel;
//using UniMana.Lib.Score;
using UniMana.Utils.Configs;
using UniMana.Utils.Data;
using UniMana.Data;
//using UniMana.Lib.Sys;

namespace UniMana.Win.Training
{
    public partial class FrmScore_SubAddStudentSubjectClassRequired : DevExpress.XtraEditors.XtraForm
    {
        #region [Declare Variables]
        private Guid m_gPersonelKey;
        private int m_iSubjectClassKey;
        private Guid m_gStudentKey;
        private DataTable m_dtStudentList = new DataTable();
        private int m_iCount = 0;
        private DBMain m_Score = new DBMain();
        #endregion
        public FrmScore_SubAddStudentSubjectClassRequired()
        {
            InitializeComponent();
        }
        public FrmScore_SubAddStudentSubjectClassRequired(Guid personelKey, int subjectClassKey, DataTable _dtStudentList)
        {
            InitializeComponent();
            m_gPersonelKey = personelKey;
            m_iSubjectClassKey = subjectClassKey;
            m_dtStudentList = _dtStudentList;
            LoadData();
        }
        //textbox
        private void txtStudentID_EditValueChanged(object sender, EventArgs e)
        {
            getInfo();
        }
        private void txtStudentID_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                getInfo();
            }
            if (e.KeyCode == Keys.Tab)
            {
                getInfo();
            }
        }
        //Methods
        private void getInfo()
        {
            if (txtStudentID != null && txtStudentID.Text != "")
            {
                Training_Student_ClassInfo infoStudentClass = new Training_Student_ClassInfo(txtStudentID.Text);
                int _iCount = 0;
                try
                {
                    
                    for (int i = 0; i < m_dtStudentList.Rows.Count; i++)
                    {
                        if (infoStudentClass.Properties.ID.ToString() == m_dtStudentList.Rows[i]["ID"].ToString())
                        {
                            _iCount++;
                        }
                    }
                }
                catch { }
                m_iCount = _iCount;
                if (_iCount != 0)
                {
                    XtraMessageBox.Show("Sinh viên đã có trong lớp!", "Thông báo");
                }
                else
                {
                    #region [Thông tin sinh vien]
                    Training_StudentInfo infoStudent = new Training_StudentInfo(infoStudentClass.Properties.Training_StudentKey.ToString());
                    m_gStudentKey = infoStudent.Properties.Training_StudentKey;
                    txtStudentName.Text = infoStudent.Properties.LastName + " " + infoStudent.Properties.SureName;
                    DateTime _dteBirthday;
                    if (DateTime.TryParse(infoStudent.Properties.Birthday.ToString(), out _dteBirthday))
                    {
                        txtBirthDay.Text = _dteBirthday.ToString("dd/MM/yyyy");
                    }
                    else
                    {
                        txtBirthDay.Text = infoStudent.Properties.ExceptionBirthday;
                    }
                    if (infoStudent.Properties.Gender == DefaultConfig.FEMALE)
                    {
                        txtGender.Text = "Nữ";
                    }
                    else if (infoStudent.Properties.Gender == DefaultConfig.MALE)
                    {
                        txtGender.Text = "Nam";
                    }
                    else
                    {
                        txtGender.Text = "";
                    }
                    try
                    {
                        Training_ClassInfo infoClass = new Training_ClassInfo(Convert.ToInt32(infoStudentClass.Properties.Training_ClassKey.ToString()));
                        txtClass.Text = infoClass.Properties.ID.ToString();
                        Training_TrainingProgramInfo infoProgram = new Training_TrainingProgramInfo(Convert.ToInt32(infoStudentClass.Properties.Training_TrainingProgramKey.ToString()));
                        txtTrainingProgram.Text = infoProgram.Properties.Name.ToString();
                    }
                    catch
                    {
                        txtClass.Text = "";
                        txtTrainingProgram.Text = "";
                    }
                    #endregion
                }
            }
        }
        private void LoadData()
        {
            Personnel_PersonnelInfo infoPersonel = new Personnel_PersonnelInfo(m_gPersonelKey);
            txtPersonelRequired.Text = infoPersonel.Properties.LastName + " " + infoPersonel.Properties.Name;
            #region [Ma phieu yeu cau]
            DataTable _dtLogAdd = new DataTable();
            _dtLogAdd = m_Score.m_DCReadOnly.Log_AddStudentSubjectClasses.GetTop1().ToDataTable();
            if (_dtLogAdd.Rows.Count != 0)
            {
                int _iID = Convert.ToInt32(_dtLogAdd.Rows[0]["Log_AddStudentSubjectClassKey"].ToString());
                _iID++;
                txtID.Text = "PYCTSV00" + _iID.ToString();
            }
            else
            {
                txtID.Text = "PYCTSV001";
            }
            #endregion
            txtBirthDay.Text = "";
            txtClass.Text = "";
            txtGender.Text = "";
            txtStudentID.Text = "";
            txtReason.Text = "";
            txtStudentName.Text = "";
            txtTrainingProgram.Text = "";
        }
        //Buttons
        private void btnRequired_Click(object sender, EventArgs e)
        {
            if (m_iCount == 0)
            {
                if (txtStudentName != null && txtStudentName.Text != "")
                {
                    if (txtReason != null && txtReason.Text != "")
                    {
                        Log_AddStudentSubjectClassInfo infoLogAdd = new Log_AddStudentSubjectClassInfo();
                        infoLogAdd.Properties.PersonelRequiredKey = m_gPersonelKey;
                        infoLogAdd.Properties.ID = txtID.Text;
                        infoLogAdd.Properties.Reason = txtReason.Text;
                        infoLogAdd.Properties.Permit = false;
                        infoLogAdd.Properties.Refuse = false;
                        infoLogAdd.Properties.Status = false;
                        infoLogAdd.Properties.DateCreate = SysDateTime.Now;
                        infoLogAdd.Properties.StudentID = txtStudentID.Text;
                        infoLogAdd.Properties.Training_StudentKey = m_gStudentKey;
                        infoLogAdd.Properties.Training_Subject_ClassKey = m_iSubjectClassKey;
                        infoLogAdd.Save();
                        XtraMessageBox.Show("Gửi yêu cầu thành công!", "Thông báo");
                        LoadData();
                    }
                    else
                    {
                        XtraMessageBox.Show("Lý do yêu cầu không được trống!","Thông báo");
                    }
                }
            }
            else
            {
                XtraMessageBox.Show("Sinh viên đã có trong lớp!","Thông báo");
            }
        }
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}